package com.lanou.controller;

import com.lanou.bean.Staff;
import com.lanou.service.StaffService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by dllo on 18/2/9.
 */
@Controller
public class MainController {
    @Resource
    private StaffService staffService;

    @RequestMapping("/login")
    public String login(@RequestParam("loginName") String loginName,
                        @RequestParam("password") String password,
                        HttpServletRequest request) {
        HttpSession session = request.getSession();
        Staff staff = staffService.login(loginName, password);
        if (staff != null) {
            session.setAttribute("admin", loginName);
            return "frame";
        } else {
            return "login";
        }
    }

    @RequestMapping("/uppassword")
    public String uppassword(@RequestParam("oldPassword") String oldPassword,
                             @RequestParam("newPassword") String newPassword,
                             @RequestParam("reNewPassword") String reNewPassword,
                             HttpServletRequest request) {
        HttpSession session = request.getSession();
        String loginName = (String) session.getAttribute("admin");
        if (oldPassword != null) {
            Staff staff = staffService.login(loginName, oldPassword);
            if (staff != null && newPassword != null && reNewPassword != null && newPassword.equals(reNewPassword)) {
                staffService.uplogin(newPassword, loginName);
                return "frame/right";
            } else {
                return "staff/updPwd";
            }
        } else {
            return "staff/updPwd";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("admin");
        return "redirect:/loginin";
    }
    @RequestMapping("/loginin")
    public String loginView() {
        return "login";
    }
    @RequestMapping("/top")
    public String top() {
        return "frame/top";
    }

    @RequestMapping("/left1")
    public String left1() {
        return "frame/left1";
    }

    @RequestMapping("/left")
    public String left() {
        return "frame/left";
    }

    @RequestMapping("/left2")
    public String left2() {
        return "frame/left2";
    }

    @RequestMapping("/right")
    public String right() {
        return "frame/right";
    }

    @RequestMapping("/control")
    public String control() {
        return "frame/control";
    }

    @RequestMapping("/updPwd")
    public String updPwd() {
        return "staff/updPwd";
    }

}


